package net.araim.bbingo.dialogs;

import java.util.Date;

import net.araim.bbingo.R;
import net.araim.bbingo.Shame;
import net.araim.bbingo.interfaces.IDialogDismissListener;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class WinnerDialog extends AlertDialog implements DialogInterface.OnKeyListener {

	public final static int DISMISS_USER = 1;
	public final static int DISMISS_SAVE = 2;

	public void setDialogDismissListener(IDialogDismissListener dialogDismissListener) {
		this.dialogDismissListener = dialogDismissListener;
	}

	private boolean save = false;
	private Date end = new Date();
	private IDialogDismissListener dialogDismissListener;

	public Date getEnd() {
		return end;
	}

	public boolean isSave() {
		return save;
	}

	public String getMeetingName() {
		return meetingName;
	}

	private String meetingName = null;

	public WinnerDialog(Context context, Date sdate) {
		super(context);

		this.setOnKeyListener(this);

		final View v = LayoutInflater.from(context).inflate(R.layout.winneralert, null);
		setView(v);

		final EditText et = (EditText) v.findViewById(R.id.MeetingName);
		final Button sb = (Button) v.findViewById(R.id.WinSaveButton);
		TextView tv = (TextView) v.findViewById(R.id.WinMeetingTimeText);
		if (sdate != null) {
			Shame s = new Shame();
			s.setSecs((int) (end.getTime() - sdate.getTime()) / 1000);
			String time = s.getTimeFormated();
			tv.setText(context.getString(R.string.Duration, time));
		}

		et.setOnKeyListener(new View.OnKeyListener() {

			public boolean onKey(View v, int keyCode, KeyEvent event) {
				sb.setEnabled(et.getText().toString().trim().length() != 0);
				return false;
			}
		});

		v.findViewById(R.id.WinDiscardButton).setOnClickListener(new View.OnClickListener() {
			public void onClick(View w) {
				dismiss();
				if (WinnerDialog.this.dialogDismissListener != null) {
					WinnerDialog.this.dialogDismissListener.notifyDismiss(WinnerDialog.this, DISMISS_USER);
				}
			}
		});

		sb.setOnClickListener(new View.OnClickListener() {
			public void onClick(View w) {
				WinnerDialog.this.save = true;
				WinnerDialog.this.meetingName = et.getText().toString();
				dismiss();
				if (WinnerDialog.this.dialogDismissListener != null) {
					WinnerDialog.this.dialogDismissListener.notifyDismiss(WinnerDialog.this, DISMISS_SAVE);
				}
			}
		});
	}

	public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			dismiss();
			if (WinnerDialog.this.dialogDismissListener != null) {
				WinnerDialog.this.dialogDismissListener.notifyDismiss(WinnerDialog.this, DISMISS_USER);
			}
			return true;
		}
		return false;
	}
}
